<?php

namespace app\admin\controller\points;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="积分记录")
 */
class Log extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\common\model\PointsLog();
        
    }

    public function index()
    {
        $userId = input('user_id', 0, 'intval');
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            if ($userId) {
                $where[] = ['p.user_id', '=', $userId];
            }
            $count = $this->model->alias('p')
                ->where($where)
                ->count();

            $list = $this->model
                ->alias('p')
                ->leftJoin('shop s','p.shop_id = s.id')
                ->leftJoin('shop s2','p.goods_shop_id = s2.id')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->order('id desc')
                ->field('p.*,s.name as shop_name,s2.name as goods_shop_name')
                ->select();
            foreach ($list as &$v) {
                if($v->type == 1){
                    $v->flag_name = '获取积分';
                }
                if($v->type == 2){
                    $v->flag_name = '消费积分';
                }
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        if ($userId) {
            $this->assign('user_id', $userId);
        }
        return $this->fetch();
    }


}